﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using ACP.Business;
using ACP.DTO;

namespace ACP.Web.UI
{
    public partial class ReviewResults : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["testId"] != null)
                {
                    int testId = Convert.ToInt32(Session["testId"]);
                    BindResults(testId);
                }
            }
        }

        private void BindResults(int testId)
        {
            try
            {
                result resultObj = ResultLogic.GetResultsWithAssociations(testId);
                List<tempexam> lstQuestions = TempExamLogic.GetExamQuestionAndAnswersByTestId(testId);
                if (resultObj != null)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append("Results for: " + resultObj.user.firstname + " " + resultObj.user.lastname);
                    sb.Append("<br /><hr><br />");
                    sb.Append("<strong>Test Name:</strong> " + resultObj.user.firstname + " " + resultObj.user.lastname);
                    sb.Append("<br /><strong>Score:</strong> " + resultObj.marks + " out of " + resultObj.examtype.noofquestions);
                    sb.Append("<br /><strong>Percentage:</strong> " + resultObj.precentage + "%");
                    sb.Append("<br /><strong>Duration:</strong> " + resultObj.timeutilized);
                    sb.Append("<br /><strong>Date started:</strong> " + resultObj.dateofstarttest.ToString());
                    sb.Append("<br /><strong>Date finished:</strong> " + resultObj.dateofendtest.ToString());
                    sb.Append("<br />");

                    if (lstQuestions != null)
                    {
                        sb.Append("<hr>");
                        sb.Append("<br />");
                        sb.Append("<br /><strong>Not Answered:</strong> " + lstQuestions.Count(x => x.SelectedAnswer == "").ToString());
                        sb.Append("<br />");
                        sb.Append("<br /><strong>Wrong Answers:</strong> " + lstQuestions.Count(x => !(bool)x.IsResult).ToString());
                        sb.Append("<br />");
                        sb.Append("<br /><strong>Correct Answeres:</strong> " + lstQuestions.Count(x => (bool)x.IsResult).ToString());
                        sb.Append("<br />");
                        sb.Append("<hr>");
                        sb.Append("<br />");
                        int questionNo = 0;
                        foreach (var item in lstQuestions)
                        {
                            questionNo++;
                            sb.Append("<hr>");
                            sb.Append("<br />");
                            sb.Append(questionNo.ToString() + ") " + item.question.question1);
                            sb.Append("A) " + item.question.option1);
                            sb.Append("<br />");
                            sb.Append("B) " + item.question.option2);
                            sb.Append("<br />");
                            if (item.question.option3 != null) sb.Append("C) " + item.question.option3); sb.Append("<br />");
                            if (item.question.option4 != null) sb.Append("D) " + item.question.option4); sb.Append("<br />");
                            if (item.question.option5 != null) sb.Append("E) " + item.question.option5); sb.Append("<br />");
                            sb.Append("<br /><strong>Selected Answer:</strong> " + item.SelectedAnswer);
                            sb.Append("<br />");
                            sb.Append("<br /><strong>Original Answer:</strong> " + item.OriginalAnswer);
                            sb.Append("<br />");
                            sb.Append("<br /><strong>Explanation:</strong> " + item.question.description);
                            sb.Append("<br />");
                            sb.Append("<hr>");
                        }
                    }
                    ltrlResult.Text = sb.ToString();


                }
            }
            catch (Exception ex)
            {
                
                Response.Write(ex.Message);
            }
           
        }
    }
}